package manifests

import (
	sredisv1 "git.blindage.org/21h/single-redis-operator/api/v1"
	v1 "k8s.io/api/core/v1"
)

func MergeLabels(itermaps ...map[string]string) map[string]string {
	result := make(map[string]string)
	for _, rv := range itermaps {
		for k, v := range rv {
			result[k] = v
		}
	}
	return result
}

func BaseLabels(cr *sredisv1.SingleRedis) map[string]string {
	return map[string]string{
		"operator": "single-redis-operator",
		"instance": cr.Name,
	}
}

func MergeENV(envs []v1.EnvVar) []v1.EnvVar {
	return append([]v1.EnvVar{
		v1.EnvVar{
			Name: "POD_NAME",
			ValueFrom: &v1.EnvVarSource{
				FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.name"},
			},
		},
		v1.EnvVar{
			Name: "POD_NAMESPACE",
			ValueFrom: &v1.EnvVarSource{
				FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"},
			},
		},
	}, envs...)
}

func ContainsString(slice []string, s string) bool {
	for _, item := range slice {
		if item == s {
			return true
		}
	}
	return false
}

func RemoveString(slice []string, s string) (result []string) {
	for _, item := range slice {
		if item == s {
			continue
		}
		result = append(result, item)
	}
	return
}
